home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 12.8 KB | 444 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UGeometry.cp
- // Copyright © 1987-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UGEOMETRY__
- #include "UGeometry.h"
- #endif
-
- // MacApp
-
- #ifndef __MACAPPTYPES__
- #include "MacAppTypes.h"
- #endif
-
- // ANSI
-
- #ifndef __LIMITS__
- #include <limits.h>
- #endif
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- #pragma segment MAGeometryRes
- //========================================================================================
- // VPoint method definitions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // Conversion method for converting a VPoint to a CPoint. Note that a newly
- // allocated CPoint is returned. We are not just viewing a VPoint as a CPoint.
- //----------------------------------------------------------------------------------------
-
- CPoint VPoint::ToPoint() const
- {
- return CPoint((short) MinMax(SHRT_MIN, h, SHRT_MAX), (short) MinMax(SHRT_MIN, v, SHRT_MAX));
- }
-
-
- //----------------------------------------------------------------------------------------
- // Arithmatic operators for VPoint. Addition and subtraction are all that make
- // any sense. Both the Add and AddTo forms are defined.
- //----------------------------------------------------------------------------------------
- VPoint VPoint::operator+(const VPoint& pt) const
- {
- return VPoint(h + pt.h, v + pt.v);
- }
-
- VPoint VPoint::operator-(const VPoint& pt) const
- {
- return VPoint(h - pt.h, v - pt.v);
- }
-
- VPoint VPoint::operator-() const
- {
- return VPoint(-h, -v);
- }
-
- VPoint& VPoint::operator+=(const VPoint& pt)
- {
- v += pt.v;
- h += pt.h;
- return *this;
- }
-
- VPoint& VPoint::operator-=(const VPoint& pt)
- {
- v -= pt.v;
- h -= pt.h;
- return *this;
- }
-
-
- //----------------------------------------------------------------------------------------
- // Relational operators for VPoint. These are defined by applying the operator in
- // question to both coordinates. The condition must hold for both to hold for the
- // VPoints the corresponding VPoints.
- //----------------------------------------------------------------------------------------
-
- Boolean VPoint::operator!=(const VPoint& pt) const
- {
- return v != pt.v || h != pt.h;
- }
-
- Boolean VPoint::operator==(const VPoint& pt) const
- {
- return v == pt.v && h == pt.h;
- }
-
- Boolean VPoint::operator>(const VPoint& pt) const
- {
- return v > pt.v && h > pt.h;
- }
-
- Boolean VPoint::operator<(const VPoint& pt) const
- {
- return v < pt.v && h < pt.h;
- }
-
- Boolean VPoint::operator>=(const VPoint& pt) const
- {
- return v >= pt.v && h >= pt.h;
- }
-
- Boolean VPoint::operator<=(const VPoint& pt) const
- {
- return v <= pt.v && h <= pt.h;
- }
-
-
- //----------------------------------------------------------------------------------------
- // Some useful methods to send to VPoints.
- //----------------------------------------------------------------------------------------
-
- void VPoint::ConstrainTo(const VRect& rt)
- {
- // If this CPoint is not inside 'rt' then move it to the nearest edge.
-
- if (v < rt.top)
- v = rt.top;
- if (v > rt.bottom)
- v = rt.bottom;
- if (h < rt.left)
- h = rt.left;
- if (h > rt.right)
- h = rt.right;
- }
-
-
- //========================================================================================
- // VRect method definitions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // Conversion method for converting a VRect to a CRect. Note that a newly allocated CRect
- // is returned. We are not just viewing a VRect as a CRect. Also a constructor for
- // constructing a VRect from a CRect.
- //----------------------------------------------------------------------------------------
-
- CRect VRect::ToRect() const
- {
- return CRect((short) MinMax(SHRT_MIN, left, SHRT_MAX),
- (short) MinMax(SHRT_MIN, top, SHRT_MAX),
- (short) MinMax(SHRT_MIN, right, SHRT_MAX),
- (short) MinMax(SHRT_MIN, bottom, SHRT_MAX));
- }
-
- //----------------------------------------------------------------------------------------
- // Operators for adding and subtracting one VRect from to/ from another. Both the Add and
- // AddTo form of operators are defined.
- //----------------------------------------------------------------------------------------
-
- VRect VRect::operator+(const VRect& rt) const
- {
- return VRect(left + rt.left, top + rt.top, right + rt.right, bottom + rt.bottom);
- }
-
- VRect VRect::operator-(const VRect& rt) const
- {
- return VRect(left - rt.left, top - rt.top, right - rt.right, bottom - rt.bottom);
- }
-
- VRect& VRect::operator+=(const VRect& rt)
- {
- top += rt.top;
- left+= rt.left;
- bottom += rt.bottom;
- right += rt.right;
-
- return *this;
- }
-
-
- VRect& VRect::operator-=(const VRect& rt)
- {
- top -= rt.top;
- left-= rt.left;
- bottom-= rt.bottom;
- right-= rt.right;
-
- return *this;
- }
-
-
- //----------------------------------------------------------------------------------------
- // Operators for adding and subtracting a VPoint to/ from a VRect. A VPoint is added to a
- // VRect by adding the VPoint to both the top-left and bottom-right VPoints that define
- // the VRect. Both the Add and AddTo operators are defined. Very convenient for
- // translating VRects. These take a CPoint and since VPoint has a constructor that takes
- // two VCoordinates the VRect windowRect can be translated 100 pixels in the positive y
- // direction by the statement:
- //
- // windowRect = windowRect + VPoint (0, 100); or
- // windowRect += VPoint (0, 100);
- //----------------------------------------------------------------------------------------
-
- VRect VRect::operator+(const VPoint& pt) const
- {
- return VRect(left + pt.h, top + pt.v, right + pt.h, bottom + pt.v);
- }
-
- VRect VRect::operator-(const VPoint& pt) const
- {
- return VRect(left - pt.h, top - pt.v, right - pt.h, bottom - pt.v);
- }
-
- VRect VRect::operator-() const
- {
- return VRect(-left, -top, -right, -bottom);
- }
-
- VRect& VRect::operator+=(const VPoint& pt)
- {
- top += pt.v;
- left+= pt.h;
- bottom+= pt.v;
- right+= pt.h;
-
- return *this;
- }
-
- VRect& VRect::operator-=(const VPoint& pt)
- {
- top -= pt.v;
- left -= pt.h;
- bottom -= pt.v;
- right -= pt.h;
-
- return *this;
- }
-
-
- //----------------------------------------------------------------------------------------
- // Inset a VRect using the coordinates in VPoint for the inset delta
- //----------------------------------------------------------------------------------------
-
- VRect& VRect::Inset(const VPoint& delta)
- {
- top += delta.v;
- left += delta.h;
- bottom -= delta.v;
- right -= delta.h;
-
- return *this;
- }
-
-
- //----------------------------------------------------------------------------------------
- // Equality operators, other relational operator could be defined such as <. But their
- // meaning is ambiguous and probably better implemented as methods. For example, aRect <
- // bRect could return true if aRect was inside of bRect, or could return true if the area
- // of aRect was less than the area of bRect.
- //----------------------------------------------------------------------------------------
-
- Boolean VRect::operator==(const VRect& rt) const
- {
- return
- top == rt.top && left == rt.left &&
- bottom == rt.bottom && right == rt.right;
- }
-
- Boolean VRect::operator!=(const VRect& rt) const
- {
- return
- top != rt.top || left != rt.left ||
- bottom != rt.bottom || right != rt.right;
- }
-
-
- //----------------------------------------------------------------------------------------
- // Two simple area operators, the intersection & and the union ||. The definition of union
- // here is to return a VRect that exactly encloses its operands.
- //----------------------------------------------------------------------------------------
-
- VRect VRect::operator &(const VRect& rt) const
- {
- VRect returnRect(Max(left, rt.left), Max(top, rt.top), Min(right, rt.right), Min(bottom, rt.bottom));
-
- if (!returnRect.Valid())
- returnRect.top = returnRect.left = returnRect.bottom = returnRect.right = 0;
-
- return VRect(returnRect);
- }
-
- VRect VRect::operator |(const VRect& rt) const
- {
- return VRect(Min(left, rt.left), Min(top, rt.top), Max(right, rt.right), Max(bottom, rt.bottom));
- }
-
-
- //----------------------------------------------------------------------------------------
- // Returns true if a valid rectangle (left < right and top < bottom). If not a valid
- // rectangle then return false and set all coordinates to 0.
- //----------------------------------------------------------------------------------------
-
- Boolean VRect::Valid() const
- {
- return left <= right && top <= bottom;
- }
-
-
- //----------------------------------------------------------------------------------------
- // Fix up coordinates so that left <= right and top <= bottom.
- //----------------------------------------------------------------------------------------
-
- void VRect::Validate()
- {
- if (top > bottom)
- {
- VCoordinate tmp = top;
- top = bottom;
- bottom = tmp;
- }
- if (left > right)
- {
- VCoordinate tmp = left;
- left = right;
- right = tmp;
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // Empty returns true if the rectangle is empty.
- //----------------------------------------------------------------------------------------
-
- Boolean VRect::Empty() const
- {
- return right <= left || bottom <= top;
- }
-
-
- //----------------------------------------------------------------------------------------
- // GetLength returns the length of a VRect in a given dimension.
- //----------------------------------------------------------------------------------------
-
- VCoordinate VRect::GetLength(VHSelect sel) const
- {
- return (sel == vSel) ? (bottom - top) : (right - left);
- }
-
-
- //----------------------------------------------------------------------------------------
- // GetSize returns the size of a VRect as a VPoint.
- //----------------------------------------------------------------------------------------
- VPoint VRect::GetSize() const
- {
- return VPoint(right - left, bottom - top);
- }
-
- //----------------------------------------------------------------------------------------
- // The Contains method takes either a VPoint or a VRect and returns true if the operand is
- // inside of the VRect the method is applied to.
- //----------------------------------------------------------------------------------------
-
- Boolean VRect::Contains (const VPoint& pt) const
- {
- // Does the CPoint 'pt' lie within the rectangle of 'this'?
-
- return pt.v >= top && pt.v < bottom && pt.h >= left && pt.h < right;
- }
-
- Boolean VRect::Contains (const VRect& rt) const
- {
- // Does the rectangle 'rt' lie withing the rectagle of 'this'?
-
- return rt.top >= top && rt.bottom <= bottom && rt.left >= left && rt.right <= right;
- }
-
- //========================================================================================
- // The following global routines were originally for Pascal compatibility. They are basically
- // wrappers that call back into the methods in VRect and VPoint. C++ programs should use
- // the methods in VRect and VPoint to avoid the additional function call overhead.
- //
- // Left in for compatibility.
- //========================================================================================
-
- void PtToVPt(const CPoint thePt, VPoint& theVPt)
- { theVPt = VPoint(thePt); }
-
- CPoint VPtToPt(const VPoint& theVPt)
- { return theVPt.ToPoint(); }
-
- void RectToVRect(const CRect& theRect, VRect& theVRect)
- { theVRect = VRect(theRect); }
-
- void VRectToRect(const VRect& theVRect, CRect& theRect)
- { theRect = theVRect.ToRect(); }
-
- void AddVPt(const VPoint& srcVPt, VPoint& dstVPt)
- { dstVPt += srcVPt; }
-
- void SubVPt(const VPoint& srcVPt, VPoint& dstVPt)
- { dstVPt -= srcVPt; }
-
- void SetVPt(VPoint& vPt, VCoordinate h, VCoordinate v)
- { vPt = VPoint(h, v); }
-
- Boolean EqualVPt(const VPoint& aVPt, const VPoint& bVPt)
- { return aVPt == bVPt; }
-
- void SetVRect(VRect& vRt,
- VCoordinate left, VCoordinate top,
- VCoordinate right, VCoordinate bottom)
- { vRt = VRect(left, top, right, bottom); }
-
- void OffsetVRect(VRect& vRt, VCoordinate dh, VCoordinate dv)
- { vRt += VPoint(dh, dv); }
-
- void InsetVRect(VRect& vRt, VCoordinate dh, VCoordinate dv)
- { vRt.Inset(VPoint(dh, dv)); }
-
- void Pt2VRect(const VPoint& theTopLeft, const VPoint& theBotRight, VRect& vRt)
- { vRt = VRect(theTopLeft, theBotRight); }
-
- Boolean PtInVRect(const VPoint& vPt, const VRect& vRt)
- { return vRt.Contains(vPt); }
-
- Boolean EmptyVRect(const VRect& vRt)
- { return vRt.Empty(); }
-
- Boolean EqualVRect(const VRect& aVRt, const VRect& bVRt)
- { return aVRt == bVRt; }
-
- VCoordinate LengthVRect(const VRect& vRt, VHSelect whichDim)
- { return vRt.GetLength(whichDim); }
-
- void PinVRect(const VRect& vRt, VPoint& vPt)
- { vPt.ConstrainTo(vRt); }
-
- Boolean SectVRect(const VRect& src1, const VRect& src2, VRect& dst)
- { dst = src1 & src2; return !dst.Empty(); }
-
- void UnionVRect(const VRect& src1, const VRect& src2, VRect& dst)
- { dst = src1 | src2; }
-
-
- //----------------------------------------------------------------------------------------
- // End of UGeometry.cp
-
- #pragma segment Inline
-